package com.lungle.warmdiary.app.view.active.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.AppContext;
import com.lungle.warmdiary.app.base.BaseDetailFragment;
import com.lungle.warmdiary.app.model.event.CampaignDetailShareEvent;
import com.lungle.warmdiary.app.model.response.Comment;
import com.lungle.warmdiary.app.view.testcampaign.CampaignDetailListTab;
import com.lungle.warmdiary.app.view.testcampaign.adapter.CampaignGridShowAdapter;
import com.lungle.warmdiary.app.view.testcampaign.view.SharePopupWindow;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.iwf.photopicker.PhotoPreview;

/**
 * Created by zhangxueyuan on 16/10/6.
 */
public class ActivityCommentDetailFragment extends BaseDetailFragment implements GridView.OnItemClickListener {


    @Bind(R.id.rate_item_name)
    TextView rateItemName;
    @Bind(R.id.rate_item_level)
    TextView rateItemLevel;
    @Bind(R.id.rate_item_ratingbar)
    RatingBar rateItemRatingbar;
    @Bind(R.id.rate_item_ratingtext)
    TextView rateItemRatingtext;
    @Bind(R.id.rate_item_content)
    TextView rateItemContent;
    @Bind(R.id.rate_item_images)
    GridView rateItemImages;
    @Bind(R.id.campaign_share)
    Button campaignShare;
    @Bind(R.id.parentPanel)
    RelativeLayout parentPanel;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_campaign_comment_detail, container, false);
        ButterKnife.bind(this, view);
        initData();
        initView(view);
        EventBus.getDefault().register(this);
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(getActivity());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getActivity());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }

    private Comment comment;
    private CampaignGridShowAdapter mAdapter;

    @Override
    public void initData() {
        comment = (Comment) getArguments().getSerializable("Comment");
    }

    @Override
    public void initView(View view) {
        if (comment != null) {
            rateItemName.setText(comment.getUser());

            int rate = Integer.parseInt(comment.getRate());

            if (rate == 0) {
                rateItemRatingbar.setVisibility(View.GONE);
                rateItemRatingtext.setVisibility(View.GONE);
            } else {
                rateItemRatingbar.setVisibility(View.VISIBLE);
                rateItemRatingtext.setVisibility(View.VISIBLE);
            }

            rateItemRatingbar.setRating(rate);
            rateItemRatingtext.setText(CampaignDetailListTab.getTitleByRate(rate));
            rateItemRatingtext.setTextColor(CampaignDetailListTab.getTabByRate(rate).color);
            rateItemContent.setText(comment.getContent());

            mAdapter = new CampaignGridShowAdapter(getActivity(), comment.getImages());
            rateItemImages.setAdapter(mAdapter);
            rateItemImages.setOnItemClickListener(this);
            mAdapter.notifyDataSetChanged();

            if (comment.getStatus() == 2 || comment.getStatus() == 3 || comment.getStatus() == 4 || comment.getStatus() == 5) {
                campaignShare.setVisibility(View.VISIBLE);
            } else {
                campaignShare.setVisibility(View.GONE);
            }

        }

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }


    private final int CODE_SHOW = 2;

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (comment != null) {
            PhotoPreview.builder()
                    .setPhotos((ArrayList) comment.getImages())
                    .setCurrentItem(position)
                    .setShowDeleteButton(false)
                    .start(getActivity(), this, CODE_SHOW);
        }

    }


    SharePopupWindow sharePopupWindow;

    @OnClick({R.id.campaign_share})
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.campaign_share:

                if (comment != null) {
                    sharePopupWindow = new SharePopupWindow(getActivity(), comment);
                }

                sharePopupWindow.showAtLocation(parentPanel, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                break;
        }
    }

    String sharetitle = "使用后感觉不错，推荐给你";

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void ShareEvent(CampaignDetailShareEvent event) {


        String content = "";
        String link = "";

        if (comment != null) {
            content = comment.getContent();
            link = comment.getSharelink();
        }


        switch (event.getId()) {
            case 0:
                share(getActivity(), sharetitle, content, link, SHARE_MEDIA.WEIXIN);
                break;
            case 1:
                share(getActivity(), sharetitle, content, link, SHARE_MEDIA.QQ);
                break;
            case 4:
                share(getActivity(), sharetitle, content, link, SHARE_MEDIA.QZONE);
                break;
        }
    }


    private void share(Activity activity, String title, String text, String link, SHARE_MEDIA platform) {
        ShareAction shareAction = new ShareAction(activity);
        if (!TextUtils.isEmpty(title)) {
            shareAction.withTitle(title);
        }
        if (!TextUtils.isEmpty(text)) {
            shareAction.withText(text);
        }
        UMImage umImage = new UMImage(getActivity(), R.drawable.ic_launcher);
        shareAction.withMedia(umImage);

        if (!TextUtils.isEmpty(link)) {
            shareAction.withTargetUrl(link);
        }
        shareAction.setPlatform(platform).setCallback(mUMShareListener).share();

    }

    private UMShareListener mUMShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA share_media) {
            AppContext.showToast("分享成功");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            AppContext.showToast(throwable.getMessage());
            throwable.printStackTrace();
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            AppContext.showToast("分享取消");
        }
    };


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(getActivity()).onActivityResult(requestCode, resultCode, data);
    }


}
